package com.atfcm.hash;

import java.util.Arrays;
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicInteger;

public class T_349_两个数组的交集 {
    public int[] intersection(int[] nums1, int[] nums2) {
        HashMap<Integer, Integer> map1 = new HashMap<>();
        for (int i = 0; i < nums1.length; i++) {
            map1.put(nums1[i], map1.getOrDefault(nums1[i], 0) + 1);
        }

        int[] res = new int[nums2.length];
        int index = 0;
        for (int i = 0; i < nums2.length; i++) {
            Integer num = map1.getOrDefault(nums2[i], 0);
            if(num > 0){
                res[index++] = nums2[i];
                num--;
                map1.put(nums2[i],num);
            }

        }
        return Arrays.copyOfRange(res, 0, index);
    }
}
